﻿$(document).ready(function () {
    page.commonUserControl = new NDUAP.Selector({ divID: "userId", typeID: 1, totalSelectNum: 1, quickBoxPositionX: -285, width: 150, searchData: page.userList });
    for (var i = 0; i < page.userList.length; i++) {
        if (page.userList[i].JobNumber == page.queryUserId) {
            page.commonUserControl.setValue([page.userList[i]]);
            break;
        }
    }

    page.init();
});

var page = {
    pageIndex: 0,
    pageSize: 15,
    url: "/handler/suggest.ashx?action=",
    popU: null,

    init: function () {
        $(".top-bar .right li:first").addClass("current");
        if (page.more == "top")
            page.showMore();
        else
            page.query();

        $("#btnQuery").click(function () {
            page.more = "";
            page.query(0);
        });
    },

    showMore: function (pageIndex) {
        if (pageIndex != undefined)
            page.pageIndex = pageIndex;

        var start = $("#start").val();
        var end = $("#end").val();

        $.get(page.url + "query", { method: page.more, pageIndex: page.pageIndex, pageSize: page.pageSize, start: start, end: end, r: Math.random() }, function (data) {
            page.pager(data.count);
            $("#result").html(TrimPath.processDOMTemplate("querymore", data));
        }, "json");
    },

    query: function (pageIndex) {
        if (page.more == "top") {
            page.showMore(pageIndex);
            return;
        }

        var id = page.commonUserControl.getValue().length > 0 ? page.commonUserControl.getValue()[0].id : 0;

        if (pageIndex != undefined)
            page.pageIndex = pageIndex;
        
        var deptName = $.trim($("#deptName").val());

        $.get(page.url + "query", { userId: id, deptName: deptName, pageIndex: page.pageIndex, pageSize: page.pageSize, r: Math.random() }, function (data) {
            $("#result").html(TrimPath.processDOMTemplate("transaction", data));
            page.pager(data.count);
        }, "json");
    },

    showComment: function (obj) {
        obj.Comment = obj.Comment.replace(/\n/g, '<br />');
        if (obj.Type == 1 && obj.FromUserId > 0)
            return obj.FromUserName + "交易素币，理由：" + obj.Comment;
        if (obj.Type == 2 && obj.FromUserId > 0)
            return "交易素币给" + obj.FromUserName + "，理由：" + obj.Comment;
        return obj.Comment;
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.query
        });
    },

    showTransfer: function () {
        var html = [];
        html.push("<table class='niceTab' width='100%' cellspacing='0' cellpadding='0' border='0'>");
        html.push("<tr><th class='three'>接受者</th><td><span id='toUserId'></span></td></tr>");
        html.push("<tr><th>交易素币</th><td><input type='text' id='toAmount' class='date' /><span>（当前可用素币：<span id='tAvail'>" + page.currentUser.available + "</span>）</span></td></tr>");
        html.push("<tr><th>交易理由</th><td><textarea class='input' id='reason'></textarea></td></tr>");
        html.push("</table>");

        page.popU = page.popU || new PopU();
        page.popU.initPop({
            type: 'string',
            title: "素币交易",
            contents: html.join(""),
            w: 500,
            y: $(window).scrollTop() + 60,
            drag: true,
            onYesFn: page.transfer,
            onNoFn: function () { }
        });
        page.popU.showPop();

        page.toUserControl = new NDUAP.Selector({ divID: "toUserId", typeID: 1, quickBoxPositionX: -180, totalSelectNum: 1, width: 150, searchData: page.userList });

        $("#toAmount").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");
    },

    transfer: function () {
        var amount = $("#toAmount").val();
        var toUserId = page.toUserControl.getValue().length > 0 ? page.toUserControl.getValue()[0] : nul;
        var reason = $("#reason").val();

        if (toUserId == null) {
            alert("请选择接受者");
            return false;
        }
        if ($.trim(amount) == "" || parseInt(amount) >= page.currentUser.available) {
            alert("交易素币必须大于0且少于您当前可用素币");
            $("#toAmount").focus();
            return false;
        }
        if ($.trim(reason) == "") {
            alert("请选择交易理由");
            $("#reason").focus();
            return false;
        }
        if (page.process) {
            alert("操作处理中，请稍后再试");
            return false;
        }

        if (confirm("当前操作不可逆，您依旧确定将" + $("#toAmount").val() + "素币交易给" + toUserId.name + "吗？")) {
            page.process = true;
            $.post(page.url + "transfer", { toUserId: toUserId.id, amount: amount, reason: reason }, function (data) {
                page.process = false;
                if (data.success) {
                    alert("操作成功");
                    page.popU.close();
                    page.query();
                    return;
                }
                alert(data.error);
            }, "json");
        }
    }
};

